package com.example.ljy;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.ljy.db.UserDao;
import com.example.ljy.model.News;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ShowWenDuActivity extends AppCompatActivity {
    RecyclerView mRecyclerView;
    MyAdapter mMyAdapter ;
    List<News> mNewsList = new ArrayList<>();
    UserDao userDao;
    Button quxian_btn;
    Button clear_data;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wendu_show_activity);
        initview();
        userDao = UserDao.getInstance(getApplicationContext());
        List<Map<String, ?>> allUsers = userDao.getAllUsers();
        if(allUsers!=null&&allUsers.size()>0){
            for (int i = 0; i < allUsers.size(); i++) {
                Map<String, ?> stringMap = allUsers.get(i);
                News news = new News();
                news.title= (String) stringMap.get("time");
                news.content= (String) stringMap.get("temperature");
                mNewsList.add(news);
            }
        }
        /*for (int i = 0; i < 50; i++) {
            News news = new News();
            news.title = "温度" + i;
            news.content = "时间" + i;
            mNewsList.add(news);
        }*/
        mMyAdapter = new MyAdapter();
        mRecyclerView.setAdapter(mMyAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(ShowWenDuActivity.this);
        mRecyclerView.setLayoutManager(layoutManager);

    }
    private void initview(){
        mRecyclerView =findViewById(R.id.wenduRecycleview);
        quxian_btn = findViewById(R.id.quxian_btn);
        clear_data= findViewById(R.id.clear_data);
        quxian_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent= new Intent(ShowWenDuActivity.this,QuXianActivity.class);
                startActivity(intent);
            }
        });
        clear_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userDao.deleteAll();
            }
        });
    }
    class MyAdapter extends RecyclerView.Adapter<MyViewHoder> {

        @NonNull
        @Override
        public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(ShowWenDuActivity.this).inflate(R.layout.wendu_show_item, parent,false);

            MyViewHoder myViewHoder = new MyViewHoder(view);
            return myViewHoder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHoder holder, int position) {
            News news = mNewsList.get(position);
            holder.mTitleTv.setText(news.title);
            holder.mTitleTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(ShowWenDuActivity.this,DetielsActivity.class);
                    startActivity(intent);
                    finish();
                }
            });
            holder.mTitleContent.setText(news.content);
        }

        @Override
        public int getItemCount() {
            return mNewsList.size();
        }
    }

    class MyViewHoder extends RecyclerView.ViewHolder {
        TextView mTitleTv;
        TextView mTitleContent;

        public MyViewHoder(@NonNull View itemView) {
            super(itemView);
            mTitleTv = itemView.findViewById(R.id.textView);
            mTitleContent = itemView.findViewById(R.id.textView2);
        }
    }
}
